﻿using System;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;

namespace TestByWire.Test
{
    [TestClass]
    public class ScenarioTests
    {
        [TestInitialize]
        public void Init()
        {
            IUnityContainer container = new UnityContainer();
            container.RegisterInstance(MockRepository.GenerateStub<IDbAccess>());
            Recorder.Reset(container);
        }

        [TestMethod, ExpectedException(typeof(ArgumentNullException))]
        public void IsShouldFailWhenInputIsNull()
        {
            Scenario.Is(null);
        }

        [TestMethod]
        public void IsShouldExecuteWhenInputIsEmpty()
        {
            Scenario.Is(EmptyFlow);
        }

        [TestMethod]
        public void IsShouldExecuteWhenInputIsACallOnly()
        {
            Scenario.Is(AFlow);
        }

        [TestMethod, ExpectedException(typeof(InvalidContextException))]
        public void IsShouldFailWhenInputIsGivenThatOnly()
        {
            Scenario.Is(GivenFlow);
        }

        [TestMethod]
        public void ItMustBePossibleToUseMultipleScenariosInSingleTest()
        {
            Scenario.Is(EmptyFlow);
            Scenario.Is(AFlow);
        }

        [TestMethod]
        public void IsShouldResetWhenInputFail()
        {
            try
            {
                Scenario.Is(GivenFlow);
            }
            catch (InvalidContextException)
            {
            }

            Given.That<IDbAccess>(x => x.GetUser()).Returns(new UserInfo());
        }

        private static void EmptyFlow()
        {
            A.CallTo<IDbAccess>(x => x.GetCurrentSalary("Foo")).Returns(3);
        }

        private static void AFlow()
        {
            A.CallTo<IDbAccess>(x => x.GetCurrentSalary("Foo")).Returns(3);
        }

        private static void GivenFlow()
        {
            Given.That<IDbAccess>(x => x.GetCurrentSalary("Foo")).Returns(3);
        }
    }
}
